//
//  HotSectionView.m
//  CarWorld
//
//  Created by yuyu on 15/3/30.
//  Copyright (c) 2015年 CarWorldTeam. All rights reserved.
//

#import "HotSectionView.h"

@implementation HotSectionView
- (void)dealloc{
   
    [_buttons release];
    [_keyView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        
    }
    return self;
}

- (void)setButtons:(NSArray *)buttons
{
    if (_buttons!=buttons) {
        [_buttons release];
        _buttons = [buttons retain];
        NSInteger count = buttons.count;
        CGFloat width = (self.frame.size.width-count*2)/(count*1.0);
        CGFloat height = self.frame.size.height;
        for (int i=0; i<count; i++) {
            UIButton *btn = _buttons[i];
            btn.frame = CGRectMake(width*i,0, width, height);
            [btn setTintColor:[UIColor blackColor]];
            [btn addTarget:self action:@selector(clickedBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = i;
            [self addSubview:btn];
            if (i==0) {
                self.keyButton = btn;
            }
        }
        CGRect rect = _keyButton.frame;
        rect.origin.y=height-2;
        rect.size.height = 2;
        rect.origin.x += 5;
        rect.size.width -= 5;
        _keyView = [[UIView alloc] initWithFrame:rect];
        _keyView.backgroundColor = [UIColor blueColor];
        [self addSubview:_keyView];
    }
}


- (void)setKeyButton:(UIButton *)keyButton
{
    if (_keyButton!=keyButton) {
        [_keyButton setTintColor:[UIColor blackColor]];
        [_keyButton release];
        _keyButton = [keyButton retain];
        [_keyButton setTintColor:[UIColor blueColor]];
    }
}

- (void)animationKeyView
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.duration = 0.1;
    
    animation.fromValue = [NSValue valueWithCGPoint:self.keyView.layer.position];
    CGFloat x = _keyButton.frame.origin.x  -  _keyView.frame.origin.x;
    CGPoint point = _keyView.layer.position;
    point.x += x+5;
    animation.toValue = [NSValue valueWithCGPoint:point];
    [self.keyView.layer addAnimation:animation forKey:@"动画名称"];
    self.keyView.layer.position = point;
}


- (void)clickedBtn:(UIButton *)btn
{
    if ([_delegate respondsToSelector:@selector(clickedSectionView:)]) {
        self.keyButton = btn;
        [self animationKeyView];
        [_delegate performSelector:@selector(clickedSectionView:) withObject:self];
    }
}



// 自绘分割线
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, rect);
    
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0xE2/255.0f green:0xE2/255.0f blue:0xE2/255.0f alpha:1].CGColor);
    CGContextStrokeRect(context, CGRectMake(10, rect.size.height - 1, rect.size.width-30, 1));
}

@end
